package com.xingda.packing.minitor.controller;


import com.xingda.packing.common.BaseController;
import com.xingda.packing.common.R;
import com.xingda.packing.config.appset.AppSetParam;
import com.xingda.packing.minitor.dto.PkLayerUploadInputDto;
import com.xingda.packing.minitor.entity.PkTask;
import com.xingda.packing.minitor.service.IPkTaskService;
import com.xingda.packing.minitor.service.IPkTaskUploadService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author anxiaoning
 * @since 2021-07-03
 */
@RestController
@RequestMapping("/api/taskUpload")
public class PkTaskUploadController extends BaseController {

    @Resource
    IPkTaskUploadService pkTaskUploadService;

    @Resource
    IPkTaskService pkTaskService;

    @PostMapping("/layer")
    public R layer(@RequestBody PkLayerUploadInputDto layerUploadInputDto) {

        try {

            if (!AppSetParam.is_floor_upload) {
                return R.error("全局配置不允许上传！");
            }

            PkTask pkTask = pkTaskService.getById(layerUploadInputDto.getTaskId());

            if (Objects.isNull(pkTask)) {
                return R.error("未查到任务信息！");
            }

            if (!pkTask.isUpload()) {
                return R.error("任务设置为不允许上传！");
            }

            if (pkTask.getType() != 1) {
                return R.error("不是成品装箱任务不允许上传！");
            }

            pkTaskUploadService.upload(layerUploadInputDto.getTaskId(), layerUploadInputDto.getLayer());
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("list")
    public R getList() {
        return R.ok().put("data",pkTaskUploadService.getList());
    }
}

